package com.mytaobao.main.firsttab.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baseproject.util.UIUtils;


/**
 * recycleView中每个item的ViewGroupBase
 */
public class ItemViewGroupBase extends LinearLayout{
    protected Context mContext;

    public ItemViewGroupBase(Context context) {
        super(context);
    }

    public ItemViewGroupBase(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemViewGroupBase(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void init(Context context, int layoutRid) {
        mContext = context;
        setOrientation(VERTICAL);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(layoutParams);
        setGravity(Gravity.CENTER);

        LayoutInflater.from(context).inflate(layoutRid, this, true);
    }

    /**
     *  水平分割线
     */
    public static void addHorizontalLine(ViewGroup parent) {
        LinearLayout.LayoutParams lpLineH = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
        View viewLineH = new View(parent.getContext());
        viewLineH.setBackgroundColor(Color.parseColor("#cccccc"));
        parent.addView(viewLineH, lpLineH);
    }
    /**
     *  垂直分割线
     */
    public static void addVerticalLine(ViewGroup parent) {
        LayoutParams lpLineV = new LayoutParams(1, LayoutParams.MATCH_PARENT);
        View viewLineV = new View(parent.getContext());
        viewLineV.setBackgroundColor(Color.parseColor("#cccccc"));
        parent.addView(viewLineV, lpLineV);
    }

    /**
     *  行LinearLayout
     */
    protected LinearLayout addLinearLayout(ViewGroup parent, int width, int height, int weight, int weightSum, int orientation) {
        LinearLayout.LayoutParams lpRow = new LinearLayout.LayoutParams(width, height);
        if(weight != 0) {
            lpRow.weight = weight;
        }
        LinearLayout llRow = new LinearLayout(mContext);
        llRow.setOrientation(orientation);
        llRow.setGravity(Gravity.CENTER);
        if(weightSum != 0) {
            llRow.setWeightSum(weightSum);
        }
        //自定义高宽比，期间调试sdk发现需要设置父LL的setBaselineAligned为false，才能在onMeasure时拿到正确的width
        llRow.setBaselineAligned(false);
        parent.addView(llRow, lpRow);
        return llRow;
    }

    /**
     * 一行灰色分隔
     * @param parent
     * @param numDP
     */
    protected  void addDivider(LinearLayout parent, int numDP) {
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, UIUtils.dip2px(numDP));
        View divider = new View(mContext);
        divider.setBackgroundColor(Color.parseColor("#f4f4f4"));
        parent.addView(divider, lp);
    }
}
